<%@ page contentType="text/html; charset=ISO-8859-1"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="it.eng.spagobi.services.proxy.*"%>
<%@ page import="it.eng.spagobi.services.session.exceptions.*"%>
<%@ taglib prefix="spagobi" tagdir="/WEB-INF/tags/spagobi" %>
<%@ include file="/html/template/template.html"%>

<%
/*
 * This software is provided under the terms
 * of GNU Lesser GPL License. You may not use
 * this file except in compliance with the license. If you need a copy of the license,
 * please go to http://www.gnu.org/copyleft/lesser.html. 
 * This code uses the SpagoBI 2.1 SDK. 
 
 * @author Fabio D'Ovidio
 * @author Matteo Villari
 
*/

// Get user and password
String user = request.getParameter("userID");
String passw = request.getParameter("password");

// proxy initialization
SessionServiceProxy proxy = new SessionServiceProxy();
proxy.setEndpoint("http://" +"localhost:8080" +"/SpagoBI/services/WSSessionService");
String error = 
"<table class='header-table-portlet-section'>"+"<tr class='header-row-portlet-section'>"
+"<td class='header-title-column-portlet-section'>Authentication ERROR</td>"
+"<td class='header-empty-column-portlet-section'>&nbsp;</td>"
+"<td class='header-button-column-portlet-section'>"
+"<a href='/SpagoBIGeoReportEngine'>"
+"<img class='header-button-image-portlet-section' title='Indietro' src='/SpagoBIGeoReportEngine/SpagoBI/logos/img/wapp/back.png' alt='Indietro' />"
+"</a></td></tr></table><div style='width:100%;text-align:center;'>"
+"<div>User or Password Invalid<br/>"
+"</div></div>";
// session opening
try {
    proxy.openSession(user, passw);
} catch (AuthenticationException e) {
    
    out.println(error);
    
    return;
}
if(proxy.isValidSession()){
%>

<script type="text/javascript">
    //<![CDATA[

var DashboardsMap = 
	{

    onPopupClose: function(evt) {
            selectControl.unselect(selectedFeature);
            
        },         
    
    onFeatureUnselect: function (feature) {
            map.removePopup(feature.popup);
            feature.popup.destroy();
            feature.popup = null;
            var infoPanel = Ext.getCmp('infotable');
            infoPanel.body.dom.innerHTML = '';
        },
      
     infotab: function(){
     
     Ext.getCmp('tab').setActiveTab('infotable');
     
     }   
    }    


  
    var indic = new Array(this.map.indicators.length);
		for(var i= 0, len = indic.length; i < len; i++){
		
      indic[i] = this.map.indicators[i];
		} 
    
    var url = this.map.url;
    var analysis = this.map.analysis;
    var layername = this.map.layer;
    var mpname = this.map.mapname;
    var server = this.map.geojsonUrl;
    var document1Label = this.map.document1Label;
    var listeners = this.map.listeners;
    var parameters = this.map.parameters;
    var mapwidth = this.map.width;
    var mapheight = this.map.height;

    var feautreInfo = this.map.feautreInfo;
    
    var getInfo = function(feature) {
    var info = "";
          for(var i=0; i<feautreInfo.length; i++){
          
            info = info+"<b>"+ feautreInfo[i][0] +"</b>: " + feature.attributes[feautreInfo[i][1]] + "<br />";                        
                
            
          
          } 
                return info;
    };
    
    var document2Label = this.map.document2Label;
    var document2Listeners = this.map.document2Listeners;
    var document2Parameters = this.map.document2Parameters;
    var lon = this.map.initPosLon;
    var lat = this.map.initPosLat;
    var zoom = this.map.initZoom;
    var layer_name = this.map.layer_name;
    var extwindow = this.map.window;

Ext.onReady(function(){
    

    
    start_map(lon,lat,zoom);
    
    
    if (analysis == "prop"){ 
    
     var propSymbolLayer = [propSymbolLayer];
      propSymbolLayer = new OpenLayers.Layer.Vector(layer_name, {
          'visibility': false,
          
          'styleMap': new OpenLayers.StyleMap({
              'select': new OpenLayers.Style(
                  {'strokeColor': 'red', 'cursor': 'pointer'}
              )
          })
      });

    map.addLayers([propSymbolLayer]);
    


    
    var spagobiDocIframe ="<iframe src='/SpagoBI/servlet/AdapterHTTP?NEW_SESSION=true&PAGE=ExecuteBIObjectPage&MODALITY=SINGLE_OBJECT_EXECUTION_MODALITY&OBJECT_LABEL="+document1Label;

    for(var i= 0, len = parameters.length; i < len; i++){
                                        //p[i] = parameters[i];
                                        var desc = parameters[i][0];
                                        var param =  parameters[i][1];
                                        spagobiDocIframe = spagobiDocIframe + "&" + desc + "=" + param ;
                                        //spagobiDocIframe = spagobiDocIframe + "&" + desc + "=" + param ;
                                        
                                      } 
                                      
    var spagobiDocIframe2 ="</br><center><iframe height='700px' width='900px' src='/SpagoBI/servlet/AdapterHTTP?NEW_SESSION=true&PAGE=ExecuteBIObjectPage&MODALITY=SINGLE_OBJECT_EXECUTION_MODALITY&OBJECT_LABEL="+document2Label;
    for(var i= 0, len = document2Parameters.length; i < len; i++){
                                        //p[i] = parameters[i];
                                        var desc = document2Parameters[i][0];
                                        var param =  document2Parameters[i][1];
                                        spagobiDocIframe2 = spagobiDocIframe2 + "&" + desc + "=" + param ;
                                        
                                      } 
    
    
    var selectPropSymbol2 = selectControl = new OpenLayers.Control.SelectFeature(propSymbolLayer,
                {onSelect: (function(feature){
          
                                  selectedFeature = feature;
                                  content =   
                                      "<div style='font-size:.8em'>"+getInfo(feature);
                                 
                                      var spagobiListnersReaction = "";
                                      for (var i=0; i<listeners.length; i++){
                                      
                                      param = feature.attributes[listeners[i][1]];
                                      desc = listeners[i][0];

                                      spagobiListnersReaction = "&"+ desc + "=" + param ;
                                      
                                      }
                                     
                                      var spagobiListnersReaction2 = "";
                                      for (var i=0; i<document2Listeners.length; i++){
                                      
                                      param = feature.attributes[document2Listeners[i][1]];
                                      desc = document2Listeners[i][0];

                                      spagobiListnersReaction2 = "&"+ desc + "=" + param ;
                                      
                                      }
                                     
                                     
                                   //spagobiContent = "";
                                   spagobiContent = spagobiDocIframe + spagobiListnersReaction + "&style='height:500px; width:100%'" + "</iframe></div>";
                                   //spagobiContent = spagobiDocIframe + 'PARAMETERS=' + spagobiListnersReaction + '&LIGHT_NAVIGATOR_DISABLED=TRUE'+'&PAGE=ExecuteBIObjectPage'+'&MESSAGEDET=EXEC_CROSS_NAVIGATION'+"</div>";
//alert (spagobiContent);
                                   spagobiContent2 = spagobiDocIframe2 + spagobiListnersReaction2 + "&style='height:500px; width:100%'"  + "</iframe></center></div>";
//alert (spagobiContent2);         

                                  var link = '<center><font size="1" face="Verdana"><a href="#" onclick="DashboardsMap.infotab();' + "Ext.getCmp('infotable').body.dom.innerHTML" + ' = spagobiContent2";>' + 'Dettagli</a></font></center>';
                                
                                  popup = new OpenLayers.Popup.FramedCloud("chicken", 
                                          feature.geometry.getBounds().getCenterLonLat(),
                                          null,
                                          content + link + spagobiContent,
                                          null, true, DashboardsMap.onPopupClose);
                                  feature.popup = popup;
                                  map.addPopup(popup);
            

          }), 
                 onUnselect: DashboardsMap.onFeatureUnselect}
          );
    
 
    map.addControl(selectPropSymbol2); 
    selectPropSymbol2.activate();

      var geostatistic = new mapfish.widgets.geostat.ProportionalSymbol({
            'map': map,
            'layer': propSymbolLayer,
            'indicators': indic,
            //'url': 'http://' + server + '/geoserver/wfs?request=GetFeature&typename=topp:'+ layername + '&outputformat=json&version=1.0.0',
            'url': '/SpagoBIGeoReportEngine/MapOl?layer=' + layername + '&server=' + server,
            'loadMask' : {msg: 'Proportional Symbols analysis...', msgCls: 'x-mask-loading'},
            'legendDiv' : 'myChoroplethLegendDiv',
            'featureSelection': false
        });
        
        
    }

    else if (analysis == "chorop"){
    
    
    choroplethLayer = new OpenLayers.Layer.Vector(layer_name, {
          'visibility': false,
          'styleMap': new OpenLayers.StyleMap({
              'default': new OpenLayers.Style(
                  OpenLayers.Util.applyDefaults(
                      {'fillOpacity': 0.5},
                      OpenLayers.Feature.Vector.style['default']
                  )
              ),
              'select': new OpenLayers.Style(
                  {'strokeColor': 'red', 'cursor': 'pointer'}
              )
          })
      });
     
    
    map.addLayer(choroplethLayer);
        
    var spagobiDocIframe ="<iframe src='/SpagoBI/servlet/AdapterHTTP?NEW_SESSION=true&PAGE=ExecuteBIObjectPage&MODALITY=SINGLE_OBJECT_EXECUTION_MODALITY&OBJECT_LABEL="+document1Label;
    
    for(var i= 0, len = parameters.length; i < len; i++){
                                        //p[i] = parameters[i];
                                        var desc = parameters[i][0];
                                        var param =  parameters[i][1];
                                        spagobiDocIframe = spagobiDocIframe + "&" + desc + "=" + param ;
                                        //spagobiDocIframe = spagobiDocIframe + "&" + desc + "=" + param ;
                                        
                                      } 
                                      
    var spagobiDocIframe2 ="</br><center><iframe height='700px' width='900px' src='/SpagoBI/servlet/AdapterHTTP?NEW_SESSION=true&PAGE=ExecuteBIObjectPage&MODALITY=SINGLE_OBJECT_EXECUTION_MODALITY&OBJECT_LABEL="+document2Label;
    for(var i= 0, len = document2Parameters.length; i < len; i++){
                                        //p[i] = parameters[i];
                                        var desc = document2Parameters[i][0];
                                        var param =  document2Parameters[i][1];
                                        spagobiDocIframe2 = spagobiDocIframe2 + "&" + desc + "=" + param ;
                                        
                                      } 
    
    
    var selectPropSymbol2 = selectControl = new OpenLayers.Control.SelectFeature(choroplethLayer,
                {onSelect: (function(feature){
          
                                  selectedFeature = feature;
                                  content =   
                                      "<div style='font-size:.8em'>"+getInfo(feature);
                                 
                                      var spagobiListnersReaction = "";
                                      for (var i=0; i<listeners.length; i++){
                                      
                                      param = feature.attributes[listeners[i][1]];
                                      desc = listeners[i][0];

                                      spagobiListnersReaction = "&"+ desc + "=" + param ;
                                      
                                      }
                                     
                                      var spagobiListnersReaction2 = "";
                                      for (var i=0; i<document2Listeners.length; i++){
                                      
                                      param = feature.attributes[document2Listeners[i][1]];
                                      desc = document2Listeners[i][0];

                                      spagobiListnersReaction2 = "&"+ desc + "=" + param ;
                                      
                                      }
                                     
                                     
                                   //spagobiContent = "";
                                   spagobiContent = spagobiDocIframe + spagobiListnersReaction + "&style='height:500px; width:100%'" + "</iframe></div>";
                                   //spagobiContent = spagobiDocIframe + 'PARAMETERS=' + spagobiListnersReaction + '&LIGHT_NAVIGATOR_DISABLED=TRUE'+'&PAGE=ExecuteBIObjectPage'+'&MESSAGEDET=EXEC_CROSS_NAVIGATION'+"</div>";
//alert (spagobiContent);
                                   spagobiContent2 = spagobiDocIframe2 + spagobiListnersReaction2 + "&style='height:500%; width:100%'"  + "</iframe><center></div>";
//alert (spagobiContent2);         

                                  var link = '<center><font size="1" face="Verdana"><a href="#" onclick="DashboardsMap.infotab();' + "Ext.getCmp('infotable').body.dom.innerHTML" + ' = spagobiContent2";>' + 'Dettagli</a></font></center>';
                                
                                
                                  popup = new OpenLayers.Popup.FramedCloud("chicken", 
                                          feature.geometry.getBounds().getCenterLonLat(),
                                          null,
                                          content + link + spagobiContent,
                                          null, true, DashboardsMap.onPopupClose);
                                  feature.popup = popup;
                                  map.addPopup(popup);
            

          }), 
                 onUnselect: DashboardsMap.onFeatureUnselect}
          );
    
 
    map.addControl(selectPropSymbol2); 
    selectPropSymbol2.activate();
      
      var geostatistic = new mapfish.widgets.geostat.Choropleth({
            'map': map,
            'layer': choroplethLayer,
            'indicators': indic,
            //'url': 'http://' + server + '/geoserver/wfs?request=GetFeature&typename=topp:'+ layername + '&outputformat=json&version=1.0.0',
            'url': '/SpagoBIGeoReportEngine/MapOl?layer=' + layername + '&server=' + server,
            'loadMask' : {msg: 'Choropleths analysis...', msgCls: 'x-mask-loading'},
            'legendDiv' : 'myChoroplethLegendDiv',
            'featureSelection': false,
            'listeners': {}
        });
  
    }
      
   else {Ext.Msg.alert( 'Status', 'No analysis category is set! Set one in template.html file!' );}   

  //TOOLBAR
  
    vectorLayer = new OpenLayers.Layer.Vector("vector", { 
                displayInLayerSwitcher: false
            });
            map.addLayer(vectorLayer);

            
            
    toolbar = new mapfish.widgets.toolbar.Toolbar({
                map: map, 
                configurable: false
            });
  
  // WINDOW
  
    // tabs for the center
        var tabs = new Ext.TabPanel({
            region    : 'center',
            margins   : '3 3 3 0', 
            id: 'tab',
            activeTab : 0,
            defaults  : {
				autoScroll : true
			},
            items: [{
                title: mpname,
                xtype: 'mapcomponent',
                map: map
                
            },{
                title    : 'Info',
                html: '<div id="info"</div>',
                id: 'infotable',
                autoScroll: true
             }]
        });

        // Panel for the west
        var nav = new Ext.Panel({
            title       : 'Navigazione',
            
            region      : 'west',
            split       : true,
            width       : 300,
            collapsible : true,
            margins     : '3 0 3 3',
            cmargins    : '3 3 3 3',
            items: [{
                      title: 'Livelli',
                      region: 'north',
                      collapsible: true,
                      autoHeight: true,
                      xtype: 'layertree',
                      map: map
                  },{
                      title: 'Toolbar',
                      collapsible: true,
                      region: 'center',
                      tbar: toolbar                
                      
                  },

                  {
                      title: 'Analisi',
                      region: 'south',
                      collapsible: true,
                      items: [geostatistic]
                      
                      
                  },
                  {
                      title: 'Legenda',
                      region: 'south',
                      collapsible: true,
                      html: '<center id="myChoroplethLegendDiv"></center>'
                      
                      
                  },
                  {
                      title: 'Logo',
                      region: 'south',
                      collapsible: true,
                      height: 85,
                      html: '<center><img src="/SpagoBIGeoReportEngine/js/lib/mapfish/georeport.png" alt="GeoReport"/></center>'
                      
                  }
                  
                  ]
        }); 
      
    
    if (extwindow == "true"){
      
      if ((mapwidth==0) || (mapheight==0)){
      var win = new Ext.Window({
            title    : 'GeoReport',
            closable : true,
            width    : 500,
            height   : 300,
            plain    : true,
            layout   : 'border',
            items    : [nav, tabs]
        });

        win.show();
      
      
      Ext.Msg.alert( 'Status', 'Please set Window dimension (height and width) in your template.html!' );

    }
    else if ((mapwidth!=0) && (mapheight!=0)){
    var win = new Ext.Window({
            title    : 'GeoReport',
            closable : true,
            width    : mapwidth,
            height   : mapheight,
            plain    : true,
            layout   : 'border',
            items    : [nav, tabs]
        });

        win.show();
    
    
    }
    
    }
    else{
    
      var viewport = new Ext.Viewport({
            title    : 'GeoReport',
            closable : true,
            width    : mapwidth,
            height   : mapheight,
            plain    : true,
            layout   : 'border',
            items    : [nav, tabs]
        });

        this.viewport=viewport;

    // });
    
    
    }

initToolbarContent();
     
});

//]]>
</script>
</html>
<%
// session closing
proxy.closeSession();
}
%>
